package com.caisejingyu.whales.home.ui

import android.annotation.SuppressLint
import android.os.Bundle
import android.widget.ImageView
import android.widget.TextView
import com.bumptech.glide.Glide
import com.bumptech.glide.load.engine.DiskCacheStrategy
import com.bumptech.glide.request.RequestOptions
import com.caisejingyu.whales.common.ui.SimpleActivity
import com.caisejingyu.whales.common.utils.FileUtil
import com.caisejingyu.whales.home.R
import com.caisejingyu.whales.home.common.Defines
import com.caisejingyu.whales.home.vm.PrintViewHelper
import java.io.File

class PhotoPaymentActivity : SimpleActivity() {

    lateinit var btnRephoto: TextView     // 重拍按钮
    lateinit var btnCancel: TextView      // 取消按钮
    lateinit var imgQrcode: ImageView     // 二维码图片
    lateinit var imgMain: ImageView     // 二维码图片

    lateinit var printVM: PrintViewHelper         // ViewModel

    override fun init(savedInstanceState: Bundle?) {
        initId()
        initArg()
        printVM = PrintViewHelper(this)
        printVM.countDownCancelStart()
        printVM.initQrCode(intent.getStringExtra("path"))
    }

    /**
     * 初始化view
     */
    fun initId() {
        // 重拍按钮
        btnRephoto = findViewById(R.id.btn_rephoto)
        btnRephoto.setOnClickListener {
            setResult(Defines.INTENT_REPHOTO)
            finish()
        }

        // 取消按钮
        btnCancel = findViewById(R.id.btn_cancel)
        btnCancel.setOnClickListener {
            setResult(Defines.INTENT_REPHOTO)
            // 取消
            finish()
        }

        // 二维码图片
        imgQrcode = findViewById(R.id.img_qrcode)
        // 主要图片
        imgMain = findViewById(R.id.img_main)
    }

    @SuppressLint("CheckResult")
    fun initArg() {
        val options = RequestOptions()
        options.skipMemoryCache(true)
        options.diskCacheStrategy(DiskCacheStrategy.NONE)
        Glide.with(this).load(intent.getStringExtra("path")).apply(options).into(imgMain)
    }

    override fun layoutId(): Int {
        return R.layout.activity_photo_payment
    }

    override fun onDestroy() {
        super.onDestroy()
        printVM.countDownCancelFinish()
        printVM.onDestory()
    }
}